<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>

        var arr1 = [8,6,10,9];

        var arr = [8, 6, 4, 2,10,9,1];
        var j, temp;
        for (var i = 1; i < arr.length; i++) {
            temp = arr[i];
            // 循环已经排好的序列
            j = i-1;
            // 你遍历到 i 元素,那肯定 左手里边 排好的最后一个元素下标 是 i-1
            while(j>=0 && temp<arr[j]){
                arr[j+1] = arr[j];  // [8, 8, 4, 2,10,9,1] , 
                j--;
                // i=2
                // [6, 8, 8, 2,10,9,1],[6, 6, 8, 2,10,9,1],    

                // 假如 遍历到9
                // [2, 4, 6, 8,10,10,1],[2, 4, 6, 8,9,10,1],
            }
            arr[j+1] = temp; // // [6, 8, 4, 2,10,9,1],[4, 6, 8, 2,10,9,1],[2, 4, 6, 8,10,9,1],
        }
        console.log(arr);



    </script>
</body>

</html>